/** @jsxImportSource @emotion/react */
import {
  CCell, Cell, InputLine, LineColumn, SuffixInput, Table, TableBody, TableHead, TableRow, Text
} from "customize-easy-ui-component";
import * as React from "react";
import {InspectRecordLayout, InternalItemProps, RepLink, SelectInput, useItemInputControl} from "../../common/base";
import {calcAverageArrObj, floatInterception, tableSetInp} from "../../../common/tool";
import {useMeasureInpFilter} from "../../common/hooks";
import {检查结论选} from "../../tools";

export const tail起升速= <Text css={{"@media print": {fontSize: '0.875rem'}}}>
  注：无载和满载最大起升速度允差为设计值±10%。
</Text>;
  //无载升 ：存储 3次数的测量对象
export const itemA起升速= [] as string[];
['无载升','无载降','满载升','满载降'].forEach((name, i: number) => {
  itemA起升速.push(name);
  itemA起升速.push(name+'v');
  itemA起升速.push(name+'r');
});
//itemA起升速 = names.flatMap(name => [name, `${name}v`, `${name}r`]);
const itemAm=itemA起升速.concat('无最起速','满最起速' );
const fuZait=['标准无载状态',  '标准载荷状态'];
const qiJian=['最大起升速度（C3.1.1）',  '最大下降速度（C3.1.2）'];
export const MaxLiftSpeed=
    React.forwardRef((
        { children, show ,alone=true,redId,nestMd,label,}:InternalItemProps,  ref
    ) => {
      const [getInpFilter]=useMeasureInpFilter(null,itemAm,);
      const {inp, setInp} = useItemInputControl({ ref });
      // const [dfh, setDfh] = React.useState<any>(inp?.无载升?.[0]?.l || '');
      const dfh=inp?.无载升?.[0]?.l || '';
      return <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show} redId={redId}
                                  nestMd={nestMd} alone={alone} label={label!}>
        <Text variant="h5">{label}：</Text>
        { fuZait.map((fzaic, i:number) => {
          return  <React.Fragment key={i}>
            { qiJian.map((shenj, j:number) => {
              const mxss= 0===i? (0===j? inp?.无最起速 :'≥300，液压单元控制的叉车对空载下降速度不作要求。') : (0===j?  inp?.满最起速 :'≤600');
              const tblnm= 0===i? (0===j?  '无载升' :'无载降') : (0===j?  '满载升' :'满载降');
              const avSpee=calcAverageArrObj(inp?.[tblnm],(row)=>(row?.l ?? dfh)/row?.t, 1,3);
              return  <React.Fragment key={j}>
                {(0!==i || 0!==j) && <hr/>}
                场景：{fuZait[i]} - {qiJian[j]} {'的测量>'}<br/>
                设计或要求值(mm/s)：{mxss} ;
                <LineColumn column={8}>
                {(new Array(3).fill(null)).map((_,t:number) => {
                  return <React.Fragment key={t}>
                    <InputLine  label={`${t+1}次测量距离`}>
                      <SuffixInput  value={inp?.[tblnm]?.[t]?.l || dfh}
                                    onChange={e =>{
                                      tableSetInp(tblnm, t, inp, setInp, 'l', e.currentTarget.value || undefined);
                                    }}>mm</SuffixInput>
                    </InputLine>
                    <InputLine  label={`${t+1}次测量时间`}>
                      <SuffixInput  value={inp?.[tblnm]?.[t]?.t || ''}
                                    onChange={e => tableSetInp(tblnm, t, inp, setInp, 't', e.currentTarget.value || undefined)}>s</SuffixInput>
                    </InputLine>
                  </React.Fragment>;
                })}
                </LineColumn>
                <LineColumn column={6}>
                  <div css={{margin: 'auto'}}>平均值 = {avSpee} mm/s</div>
                  <InputLine  label='测定结果' >
                    <SuffixInput  value={inp?.[tblnm+'v'] ||''}
                                  onChange={e=>setInp({ ...inp, [tblnm+'v']: e.currentTarget.value||undefined})}>mm/s</SuffixInput>
                  </InputLine>
                  <InputLine label='结论判定'>
                    <SelectInput value={inp?.[tblnm+'r'] || ''} list={检查结论选}
                                 onChange={e=>setInp({ ...inp, [tblnm+'r']: e.currentTarget.value||undefined})}/>
                  </InputLine>
                </LineColumn>
              </React.Fragment>;
             })}
          </React.Fragment>;
        } ) }
        {tail起升速}
      </InspectRecordLayout>;
});

export const MaxLiftSpeedVw= ({ orc, rep,label,} : { orc: any, rep: any,label:string}
) => {
  const dfh=orc?.无载升?.[0]?.l || '';
  return <>
    <Text id='MaxLiftSpeed' variant="h4" css={{marginTop: '1rem',
    }}>{label}</Text>
    <Table fixed={ ["3.2%","6%","11%","10%","12%","10%","11%","10%","12%","%"]  }  css={ {borderCollapse: 'collapse' } } tight  miniw={800}>
      <TableHead>
        <TableRow>
          <CCell rowSpan={3}>序号</CCell><CCell rowSpan={3}>试验状态</CCell><CCell colSpan={4}>{qiJian[0]}</CCell><CCell colSpan={4}>{qiJian[1]}</CCell>
        </TableRow>
        <TableRow>
          <CCell>测量距离</CCell><CCell>测量时间</CCell><CCell>测量值</CCell><CCell>设计值</CCell>
          <CCell>测量距离</CCell><CCell>测量时间</CCell><CCell>测量值</CCell><CCell>设计值或要求值</CCell>
        </TableRow>
        <TableRow>
          <CCell>mm</CCell><CCell>s</CCell><CCell colSpan={2}>mm/s</CCell><CCell>mm</CCell><CCell>s</CCell><CCell colSpan={2}>mm/s</CCell>
        </TableRow>
      </TableHead>
      <TableBody>
        <RepLink ori rep={rep} tag={'MaxLiftSpeed'}>
          { fuZait.map((fzaic, i:number) => {
            const tbn=0===i? '无载' : '满载';
            const avSpee1=calcAverageArrObj(orc?.[tbn+'升'],(row)=>(row?.l ?? dfh)/row?.t, 1,3);
            const avSpee2=calcAverageArrObj(orc?.[tbn+'降'],(row)=>(row?.l ?? dfh)/row?.t, 1,3);
            return  <React.Fragment key={i}>
              {(new Array(3).fill(null)).map((_,t:number) => {
                let dvdrs1= Number(orc?.[tbn+'升']?.[t]?.l || dfh) / Number(orc?.[tbn+'升']?.[t]?.t || '');
                const ospv1=isFinite(dvdrs1)? floatInterception(dvdrs1, 1) :'';
                let dvdrs2= Number(orc?.[tbn+'降']?.[t]?.l || dfh) / Number(orc?.[tbn+'降']?.[t]?.t || '');
                const ospv2=isFinite(dvdrs2)?  floatInterception(dvdrs2, 1) :'';
                return <TableRow key={t}>
                    <CCell>{t+1}</CCell>{0===t && <CCell rowSpan={3}>{fzaic}</CCell>}
                    <CCell>{orc?.[tbn+'升']?.[t]?.l || dfh}</CCell><CCell>{orc?.[tbn+'升']?.[t]?.t || ''}</CCell>
                    <CCell>{ospv1}</CCell>
                    {0===t && <CCell rowSpan={4}>{0===i? orc?.无最起速 : orc?.满最起速}</CCell>}
                    <CCell>{orc?.[tbn+'降']?.[t]?.l || dfh}</CCell><CCell>{orc?.[tbn+'降']?.[t]?.t || ''}</CCell>
                    <CCell>{ospv2}</CCell>
                    {0===t && <CCell rowSpan={4}>{0===i? '≥300，液压单元控制的叉车对空载下降速度不作要求。' : '≤600'}</CCell>}
                  </TableRow>;
              })}
              <TableRow>
                <CCell colSpan={2}>平均值</CCell><CCell colSpan={2}>—</CCell><CCell>{avSpee1}</CCell>
                <CCell colSpan={2}>—</CCell><CCell>{avSpee2}</CCell>
              </TableRow>
              <TableRow>
                <CCell colSpan={4}>测定结果（{tbn}）</CCell><CCell colSpan={2}>{orc?.[tbn+'升v'] ||''}</CCell>
                <CCell colSpan={2}>测定结果（{tbn}）</CCell><CCell colSpan={2}>{orc?.[tbn+'降v'] ||''}</CCell>
              </TableRow>
              <TableRow>
                <CCell colSpan={2}>结果判定</CCell><CCell colSpan={4}>{orc?.[tbn+'升r'] ||''}</CCell><CCell colSpan={4}>{orc?.[tbn+'降r'] ||''}</CCell>
              </TableRow>
            </React.Fragment>;
          } ) }
          <TableRow>
            <Cell colSpan={10} css={{whiteSpace: 'pre-wrap'}}>{tail起升速}</Cell>
          </TableRow>
        </RepLink>
      </TableBody>
    </Table>
  </>;
};
